package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import Json.forLogin;

/**
 * Servlet implementation class DoLogin
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out;
		out = response.getWriter();
		String jsonText = request.getParameter("jsonText");
		String name = null;
		String pass = null;
		try {
			name = forLogin.process(jsonText).getLoginName();
			pass = forLogin.process(jsonText).getLoginPass();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String result = DBFunction.fLogin.check(name, pass);
		if(result.equals("is_online"))
			out.print("2");
		else if (result.equals("right"))
			out.print("1");
		else if (result.equals("wrong"))
			out.print("0");
		else if (result.equals("is_inactive"))
			out.print("3");
		else
			out.print("-1");

	}

}
